//
//  TTMacro.h
//  Tint
//
//  Created by Kevin Wu on 11/8/11.
//  Copyright (c) 2011 Telligenty. All rights reserved.
//


/*
 * Singleton
 */
#define TTSynthesizeSingleton(classname) \
static classname *shared##classname = nil; \
+ (classname *)sharedObject \
{ \
  if ( shared##classname == nil ) {\
    shared##classname = [[self alloc] init]; \
  } \
  return shared##classname; \
} \
+ (BOOL)hasSharedObject \
{ \
  return ( shared##classname != nil ); \
} \
+ (void)destroySharedObject \
{ \
  if ( shared##classname ) { \
    [shared##classname release]; \
    shared##classname = nil; \
  } \
} \
- (id)copyWithZone:(NSZone *)zone { return self; } \
- (id)retain { return self; } \
- (NSUInteger)retainCount { return NSUIntegerMax; } \
- (oneway void)release { } \
- (id)autorelease { return self; }

/*
 * Variable argument lists
 */
#define TTValistToArray(first, container) \
if ( first ) { \
  container = [[NSMutableArray alloc] init]; \
  id object; \
  va_list list; \
  va_start(list, first); \
  while ( (object = va_arg(list, id)) ) {\
    [container addObject:object]; \
  } \
  va_end(list); \
}


/*
 * Memory management
 */
#define TTRetain(__new, __old)      { [__new retain]; [__old release]; __old = __new; }
#define TTCopy(__new, __old)        { if ( __new != __old ) { [__old release]; __old = [__new copy]; } }
#define TTRelease(__pointer)        { [__pointer release]; __pointer = nil; }
#define TTReleaseCF(__ref)          { if ( (__ref) != nil ) { CFRelease(__ref); __ref = nil; } }
#define TTInvalidateTimer(__timer)  { [__timer invalidate]; __timer = nil; }

#define TTBeginAutoreleasePool() NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init]
#define TTEndAutoreleasePool() [autoreleasePool release]

/*
 * TimeInterval
 */
#define TTTimeIntervalMinute() (60)
#define TTTimeIntervalHour() (60 * 60)
#define TTTimeIntervalDay() (24 * 60 * 60)
#define TTTimeIntervalWeek() (7 * 24 * 60 * 60)

